$(function(){
    // 冻结
    $('.frozen').on('click',function(){
        var _this= $(this);
        var hotel_name =  $(this).parents('tr').find('td').eq(0).text();
		var hotel_account = $(this).parents('tr').find('td').eq(1).text();
        var hotel_code = $('input[name=hotel_code]').val();
		$.confirm({
			msg: '确定冻结？',
			confirm: function(){
                $.post(`/Admin/Hotel/hotelAdminDel/1`,{name: hotel_name,hotel_code: hotel_code,account:  hotel_account},function(info){
					info = JSON.parse(info);
					if(info.code==1) {
                        $.alert('冻结成功');
                        _this.siblings('.thaw').show();	
                        _this.parents('tr').find('td').eq(2).text('已冻结');
                        _this.hide();
                        _this.siblings('.reset-word').hide();	
					}else {
						$.alert(info.msg);
					}
				})
			}
		})
    })
    // 解冻
    $('.thaw').on('click',function(){
        var _this = $(this);
		var hotel_account = $(this).parents('tr').find('td').eq(1).text();
        var hotel_code = $('input[name=hotel_code]').val();
        var hotel_name =  $(this).parents('tr').find('td').eq(0).text();
		$.confirm({
			msg: '确定解冻？',
			confirm: function(){
				$.post(`/Admin/Hotel/hotelAdminDel/0`,{name: hotel_name,hotel_code: hotel_code,account:  hotel_account},function(info){
					info = JSON.parse(info);
					if(info.code==1) {
                        $.alert('解冻成功');
                        _this.siblings('.frozen').show();
                        _this.siblings('.reset-word').show();	
                        _this.parents('tr').find('td').eq(2).text('正常');
                        _this.hide()
                     
					}else {
						$.alert(info.msg);
					}
				})
			}
		})
    })
    // 重置密码
    $('.reset-word').on('click',function(){
        var _this = $(this);
        var hotel_name =  $(this).parents('tr').find('td').eq(0).text();
        var hotel_account = $(this).parents('tr').find('td').eq(1).text();
        var hotel_code = $('input[name=hotel_code]').val();
        $.post(`/Admin/Hotel/hotelAdminEdit`,{name: hotel_name,hotel_code: hotel_code,account:  hotel_account},function(info){
            info = JSON.parse(info);
            console.log(info);
            if(info.code==1) {
                $.alert('重置成功，密码已通过短信发送');
            }else {
                $.alert(info.msg);
            }
        })
    })
    // 添加账号
    $('.add-account').on('click',function(){
        var str = '<tr>'+
            '<td><input class="add-name" name="name" type="text" required></td>'+
            '<td><input isphone="isphone" class="add-input" type="number" required></td>'+
            '<td>******</td>'+
            '<td>正常</td>'+
            '<td class="blue-color">'+
                '<span style="cursor: pointer;" class="blue-color save-account">发送短信密码</span>'+
                '<span style="display: none;margin-right: 10px;cursor: pointer;" class="blue-color reset-word">重置密码</span>'+
                '<span style="display: none;cursor: pointer;" class="blue-color frozen">冻结</span>'+
                '<span style="display: none;cursor: pointer;" class="blue-color thaw">解冻</span>'+
            '</td>'+
        '</tr>';
        $('#account-table').find('tbody').append(str);
        $('#account-table').find('tbody').find('tr:last').find('td:first input').focus();
    })

    // 保存
    $('#account-table').on('click','.save-account',function(){
        var _this = $(this);
        var hotel_name = $(this).parents('tr').find('.add-name').val();
        var hotel_account = $(this).parents('tr').find('.add-input').val();
        var hotel_code = $('input[name=hotel_code]').val();
        // console.log(hotel_account,hotel_code)
        var reg = /^((13|14|15|17|18|16|19)[0-9]{1}\d{8})$/;
        if(hotel_account==''||!reg.test(hotel_account)){
            $.alert('请输入正确的手机号码');
            return;
        }
        $.post(`/Admin/Hotel/hotelAdmin`,{name: hotel_name,hotel_code: hotel_code,account:  hotel_account},function(info){
            info = JSON.parse(info);
            // console.log(info);

            if(info.code==1) {
                $.alert('保存成功');
                _this.hide().siblings('.frozen').show().siblings('.reset-word').show();    
            }else {
                $.alert(info.msg);
            }
        })
    })
   
    // 表单提交
    var Form = $('#main-form');
    Form.validate();
    Form.on('submit',function() {
		try {
			if (Form.valid()) {
                if($("#province option:selected").text()==''||$("#city option:selected").text()==""||$("#area option:selected").text()==""||$('.address_detail').val()=='') {
                    $.alert('请填写地址！')
                    return false;
                }
                // if(ue.getContent()=='') {
                //     $.alert('请填写图文详情！')
                //     return false;
                // }
                var reg = /^((0\d{2,3}-\d{7,8})|(1[37584]\d{9}))$/;

                 if($('input[name=phone]').val()&&!reg.test($('input[name=phone]').val())){
                    $.alert('请输入正确的联系电话');
                    return false;
                }
                
				Form.submitForm(function(info){
                    if(info.code==1){
                        setTimeout(function(){
                               history.back(-1);
                        },800)
                     
                    }else {
                        $.alert(info.msg);
                    }
                    return;
                });
			}
		} catch(e) {
			console.error(e);
        }
  
		return false;
    });
})